package com.vehicle_information_management_system.controller;


<<<<<<< HEAD
=======
import cn.hutool.crypto.digest.MD5;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
>>>>>>> 629a544 (update)
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.vehicle_information_management_system.pojo.UserInfo;
import com.vehicle_information_management_system.service.impl.UserInfoServiceImpl;
import com.vehicle_information_management_system.utils.JwtUtil;
import com.vehicle_information_management_system.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;


/**
 * @author huawei
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private UserInfoServiceImpl userInfoService;
    @RequestMapping("/login")
    public Result<?> userLogin(@RequestParam String username, @RequestParam String password) {
        try {

            UserInfo userInfo=userInfoService.userLogin(username,password);
            Map<String,Object> map=new HashMap<>();
            map.put("userId",userInfo.getUserId());
            map.put("username",userInfo.getUsername());
            String token = JwtUtil.generateToken(userInfo.getUsername(), userInfo.getUserId());
            map.put("token", token);
            return Result.success("登录成功",map);
        } catch (Exception e) {
            return Result.error("登录失败");
        }
    }
    @RequestMapping("/add")
    public Result<?> addUser(@RequestBody UserInfo userInfo) {
        try {
            userInfoService.addUser(userInfo);
            return Result.success("添加成功");
        } catch (Exception e) {
            return Result.error("添加失败");
        }
    }
    @RequestMapping("/del/{id}")
    public Result<?> delUser(@PathVariable Integer id) {
        try {
            userInfoService.delUser(id);
            return Result.success("删除成功");
        } catch (Exception e) {
            return Result.error("删除失败");
        }
    }
    @RequestMapping("/update")
    public Result<?> updateUser(@RequestBody UserInfo userInfo) {
        try {
            userInfoService.updateUser(userInfo);
            return Result.success("修改成功");
        } catch (Exception e) {
            return Result.error("修改失败");
        }
    }
    @RequestMapping("/list")
    public Result<?> getAllUser(@RequestParam(defaultValue = "1") Integer pageNum,
                                @RequestParam(defaultValue = "10") Integer pageSize,
                                @RequestParam(defaultValue = "") String search) {
        try {
            IPage<UserInfo> page=userInfoService.getAllUser(pageNum, pageSize, search);
            log.info("page:{}",page);
            return Result.success("获取成功",page);
        } catch (Exception e) {
            return Result.error("获取失败");
        }
    }
    @RequestMapping("/getUserInfoById/{id}")
    public Result<?> getUserInfoById(@PathVariable Integer id) {
        try {
            UserInfo userInfo=userInfoService.getUserInfoById(id);
            return Result.success("获取成功",userInfo);
        } catch (Exception e) {
            return Result.error("获取失败");
        }
    }
<<<<<<< HEAD
=======

    @RequestMapping("/resetPassword")
    public Result<?> resetPassword(@RequestParam Integer userId, @RequestParam String oldPassword, @RequestParam String newPassword)  {
        try {
            String password=userInfoService.getUserInfoById(userId).getPassword();
            UserInfo userInfo=userInfoService.getUserInfoById(userId);
            if(!(MD5.create().digestHex(oldPassword).equals(password))){
                return Result.error("原密码错误");
            }
            userInfo.setPassword(MD5.create().digestHex(newPassword));
            userInfoService.update(userInfo,new QueryWrapper<UserInfo>().eq("user_id",userId));
            return Result.success("重置成功");
        } catch (Exception e) {
            return Result.error("重置失败");
        }
    }
>>>>>>> 629a544 (update)
}
